package com.ljfiction.book.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ljfiction.book.Baner.HotbiaoqianBean;
import com.ljfiction.book.MyView.BiaoqianBeijing;
import com.ljfiction.book.R;
import com.ljfiction.book.activitys.BiaoqianActivity;
import com.ljfiction.book.activitys.ClassfimuchActivity;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.readview.BookReadContract;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;

public class BiaoqianFragment extends BaseFragment{


    private TextView hotbiaoqiantextview1;

    private RelativeLayout hotview1;

    private TextView hotbiaoqiantextview2;

    private RelativeLayout hotview2;

    private TextView hotbiaoqiantextview3;

    private RelativeLayout hotview3;

    private RecyclerView hotbiaoqianmorerecycleview;

    private SmartRefreshLayout refreshBiaoqianfragment;

    private Context context;
    private List<HotbiaoqianBean.DataBean> classifydatas = new ArrayList<>();
    private List<HotbiaoqianBean.DataBean> allhotbiaoqian = new ArrayList<>();
    private BaseQuickAdapter homClassifyAdapter;
    private HotbiaoqianBean f;
    private Intent intent;
    private NestedScrollView nestedScrollView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hotbiaoqian, container, false);
        hotbiaoqiantextview1 = view.findViewById(R.id.hotbiaoqiantextview1);
        hotbiaoqiantextview2 = view.findViewById(R.id.hotbiaoqiantextview2);
        hotbiaoqiantextview3 = view.findViewById(R.id.hotbiaoqiantextview3);
        hotview1 = view.findViewById(R.id.hotview1);
        hotview2 = view.findViewById(R.id.hotview2);
        hotview3 = view.findViewById(R.id.hotview3);
        hotbiaoqianmorerecycleview = view.findViewById(R.id.hotbiaoqianmorerecycleview);
        refreshBiaoqianfragment = view.findViewById(R.id.refresh_biaoqianfragment);
        nestedScrollView=view.findViewById(R.id.biaoqiannextedscroolview);
        context = getActivity();
        intView();
        intevent();
        initData();
        return view;
    }

    private void intevent() {
        hotview1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent = new Intent(context, ClassfimuchActivity.class);
                intent.putExtra("id", String.valueOf(allhotbiaoqian.get(0).getId()));
                intent.putExtra("name", allhotbiaoqian.get(0).getBookLableName());
                context.startActivity(intent);
            }
        });
        hotview2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent = new Intent(context, ClassfimuchActivity.class);
                intent.putExtra("id", String.valueOf(allhotbiaoqian.get(1).getId()));
                intent.putExtra("name", allhotbiaoqian.get(1).getBookLableName());
                context.startActivity(intent);
            }
        });
        hotview3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent = new Intent(context, ClassfimuchActivity.class);
                intent.putExtra("id", String.valueOf(allhotbiaoqian.get(2).getId()));
                intent.putExtra("name", allhotbiaoqian.get(2).getBookLableName());
                context.startActivity(intent);
            }
        });

    }


    private void intView() {
        hotbiaoqianmorerecycleview.setAdapter(homClassifyAdapter = new BaseQuickAdapter<HotbiaoqianBean.DataBean, BaseViewHolder>(R.layout.biaoqianbeijing, classifydatas) {
            @Override
            protected void convert(BaseViewHolder helper, HotbiaoqianBean.DataBean item) {
                AutoUtils.autoSize(helper.itemView);
                helper.setText(R.id.biaoqianbeitext, item.getBookLableName());
                ((BiaoqianBeijing) helper.getView(R.id.biaoqianbeijingtext)).setBackcolor(item.getBookColor());

            }
        });
        homClassifyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Intent intent = new Intent(context, ClassfimuchActivity.class);
                intent.putExtra("id", String.valueOf(classifydatas.get(position).getId()));
                intent.putExtra("name", classifydatas.get(position).getBookLableName());
                context.startActivity(intent);
            }
        });
        hotbiaoqianmorerecycleview.setLayoutManager(new GridLayoutManager(context, 4));
        refreshBiaoqianfragment.setEnableLoadMore(false);
        refreshBiaoqianfragment.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {

            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                classifydatas.clear();
                allhotbiaoqian.clear();
                initData();
            }
        });
    }


    private void initData() {

        OkHttpUtils.get(HttpManager.IP + "/stacks-hotLableVersionTow")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        refreshBiaoqianfragment.finishRefresh();
                        Gson g = new Gson();
                        try {
                            f = g.fromJson(s, HotbiaoqianBean.class);
                            allhotbiaoqian.addAll(f.getData());
                            for (int i = 3; i < allhotbiaoqian.size(); i++) {
                                classifydatas.add(allhotbiaoqian.get(i));
                            }
                            homClassifyAdapter.notifyDataSetChanged();
                            tochanggehot();
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void tochanggehot() {
        if (allhotbiaoqian.size()>0){
            if (allhotbiaoqian.get(0).getBookLableName().length() > 3) {
                hotbiaoqiantextview1.setMaxEms(2);

            } else {
                hotbiaoqiantextview1.setMaxEms(3);
            }
            hotbiaoqiantextview1.setText(allhotbiaoqian.get(0).getBookLableName());
            if (allhotbiaoqian.size()>1){
                if (allhotbiaoqian.get(1).getBookLableName().length() > 3) {
                    hotbiaoqiantextview2.setMaxEms(2);
                } else {
                    hotbiaoqiantextview2.setMaxEms(3);
                }
                hotbiaoqiantextview2.setText(allhotbiaoqian.get(1).getBookLableName());
                if (allhotbiaoqian.size()>2){
                    if (allhotbiaoqian.get(2).getBookLableName().length() > 3) {
                        hotbiaoqiantextview3.setMaxEms(2);
                    } else {
                        hotbiaoqiantextview3.setMaxEms(3);
                    }
                    hotbiaoqiantextview3.setText(allhotbiaoqian.get(2).getBookLableName());
                }
            }
        }
        if (allhotbiaoqian.size()<3){
            hotbiaoqiantextview3.setVisibility(View.GONE);
            if (allhotbiaoqian.size()<3){
                hotbiaoqiantextview2.setVisibility(View.GONE);
                if (allhotbiaoqian.size()<1){
                    hotbiaoqiantextview1.setVisibility(View.GONE);
                }
            }
        }







    }




}
